<?php
/**
 * Created by PhpStorm.
 * User: zl
 * Date: 2017/6/17
 * Time: 11:48
 */
    require_once ('Common.php');
    require_once ('SQLBase.php');
    class UserInfo{
        public function getBasicLoginUser($user, $pwd){
            //返回结果数组
            $result_array = array();
            $db = new SQLBase();
            $queryStr = "SELECT user.*, executive.name FROM user, executive WHERE user_ID= '$user' and user_pwd='$pwd' and executive.level = user.executive_level";
            $result = $db->queryData($queryStr);

            if(!$result){
                $result_array['state'] = 0;
                $result_array['error'] = 'DB_ERROR';
            }
            else {
                //如果没有这个用户，返回错误信息
                if ($result->num_rows == 0) {
                    $result_array['state'] = 0;
                    $result_array['error'] = 'NO_SUCH_USER';
                }
                else{
                    $temp = $result->fetch_array();
                    //记录当前用户
                    $result_array['state'] = 1;
                    $result_array['user_id'] = $temp['user_ID'];
                    $result_array['user_name'] = $temp['user_name'];
                    $result_array['user_bank_card'] = $temp['card_number'];
                    $result_array['user_identity_card'] = $temp['user_number'];
                    $result_array['executive_level'] = $temp['executive_level'];
                    $result_array['executive_name'] = $temp['name'];
                }
            }
            return $result_array;
        }

        public function isSysAdmin($user, $pwd){
            $conn = new SQLBase();
            $queryStr = "SELECT * FROM admin WHERE admin_ID = '$user' AND admin_pwd = '$pwd'";

            $result = $conn->queryData($queryStr);
            if($result->num_rows == 0){
                return 0;
            }
            else{
                return 1;
            }
        }

        public function isFinanceAdmin($user){
            $conn = new SQLBase();
            $queryStr = "SELECT * FROM finance_admin WHERE user_ID = '$user'";
            $result = $conn->queryData($queryStr);
            if($result->num_rows == 0){
                return false;
            }
            return true;
        }

        public function getDepartmentInfo($user){
            $db = new SQLBase();
            //查询用户部门信息
            $queryStr = "SELECT section.*, allsection.section_name, allsection.section_type FROM section, allsection WHERE 
                        section.user_ID = '$user' AND section.section_ID = allsection.section_ID AND allsection.section_state=1";
            $result_array = array();
            $result = $db->queryData($queryStr);

            //部门信息构成数组存放
            if($result->num_rows > 0){
                for($i = 0; $i < $result->num_rows; $i++){
                    $data = $result->fetch_array();
                    $result_array[$i] = array('department_id'=>$data['section_ID'], 'department_type'=>$data['section_type'],
                        'department_identity'=>$data['section_job'], 'department_name'=>$data['section_name']);
                }
            }
            return $result_array;
        }



        public function getApprovePowerAdmin($user){

            $admin_power = array('state'=>0, 'section_approve'=>0, 'section_fee'=>0, 'section_data'=>array(), 'manage_approve'=>0, 'manage_fee'=>0, 'leader_approve'=>0, 'leader_fee'=>0);
            $this->_getSectionPower($user, $admin_power);
            $this->_getManagePower($user, $admin_power);
            $this->_getLeaderPower($user, $admin_power);
            return $admin_power;
        }

        private function _getSectionPower($user, &$admin_power){
            $conn = new SQLBase();
            $queryStr = "SELECT * FROM leader WHERE user_id='$user' AND power_level=1";
            $result = $conn->queryData($queryStr);
            if($result){
                $admin_power['state'] = 1;
                if($result->num_rows > 0){
                    for($i = 0; $i < $result->num_rows; $i++){
                        $row = $result->fetch_array();
                        if($row['is_approve'] == 1){
                            $admin_power['section_approve'] = 1;
                        }
                        if($row['is_fee'] == 1){
                            $admin_power['section_fee'] = 1;
                        }
                    }
                }
            }
            else{
                $admin_power['state'] = 0;
            }
        }

//        private function _getSectionPower($user, &$admin_power){
//            $conn = new SQLBase();
//            $queryStr = "SELECT leader.*, allsection.section_name FROM leader, allsection WHERE user_id='$user' AND power_level=1 AND leader.section_id = allsection.section_ID";
//            $result = $conn->queryData($queryStr);
//            if($result){
//                $admin_power['state'] = 1;
//                if($result->num_rows > 0){
//                    for($i = 0; $i < $result->num_rows; $i++){
//                        $row = $result->fetch_array();
//                        $admin_power['section_data'][$i] = array('section_id'=>$row['section_id'], 'section_name'=>$row['section_name'], 'is_approve'=>$row['is_approve'], 'is_fee'=>$row['is_fee']);
//                    }
//                }
//            }
//            else{
//                $admin_power['state'] = 0;
//            }
//        }

        private function _getManagePower($user, &$admin_power){
            $conn = new SQLBase();
            $queryStr = "SELECT * FROM leader WHERE user_id='$user' AND power_level=2";
            $result = $conn->queryData($queryStr);
            if($result){
                $admin_power['state'] = 1;
                if($result->num_rows > 0){
                    for($i = 0; $i < $result->num_rows; $i++){
                        $row = $result->fetch_array();
                        if($row['is_approve'] == 1){
                            $admin_power['manage_approve'] = 1;
                        }
                        if($row['is_fee'] == 1){
                            $admin_power['manage_fee'] = 1;
                        }
                    }
                }
            }
            else{
                $admin_power['state'] = 0;
            }
        }

        private function _getLeaderPower($user, &$admin_power){
            $conn = new SQLBase();
            $queryStr = "SELECT * FROM leader WHERE user_id='$user' AND power_level=3";
            $result = $conn->queryData($queryStr);
            if($result){
                $admin_power['state'] = 1;
                if($result->num_rows > 0){
                    for($i = 0; $i < $result->num_rows; $i++){
                        $row = $result->fetch_array();
                        if($row['is_approve'] == 1){
                            $admin_power['leader_approve'] = 1;
                        }
                        if($row['is_fee'] == 1){
                            $admin_power['leader_fee'] = 1;
                        }
                    }
                }
            }
            else{
                $admin_power['state'] = 0;
            }
        }



        public function getApprovePower($user){
            $conn = new SQLBase();
            //查询审批信息
            $queryStr = "SELECT * FROM leader WHERE user_id='$user'";
            $result = $conn->queryData($queryStr);
            $admin_power = array();
            if($result->num_rows > 0){
                for($i = 0; $i < $result->num_rows; $i++){
                    $data = $result->fetch_array();
                    $admin_power[$i] = array('is_approve'=>$data['is_approve'], 'is_fee'=>$data['is_fee'], 'power_level'=>$data['power_level']);
                    $admin_power[$i]['section_power'] = array();
                    $section = $data['section_id'];
                    $query = 'SELECT section_ID, section_name FROM allsection WHERE section_state = 1';
                    if($section != 'all'){
                      //  $s = explode(',', $section);
                        $query .= " AND section_ID in (".$section.")";
//                        for($j = 0; $j < count($s); $j++){
//                            if($j < count($s) - 1){
//                                $query .= "'$s[$j]',";
//                            }
//                            else{
//                                $query .= "'$s[$j]')";
//                            }
//                        }
                    }
                    $section_result = $conn->queryData($query);
                    for($j = 0; $j < $section_result->num_rows; $j++){
                        $row = $section_result->fetch_array();
                        $admin_power[$i]['section_power'][$j] = array('section_id'=>$row['section_ID'], 'section_name'=>$row['section_name']);
                    }
                }


                return $admin_power;
            }
            else{
                return false;
            }

        }
        public function  getLeaderAllUserData(){
            $db=new SQLBase();
            $queryStr="SELECT DISTINCT leader.user_id,`user`.user_name FROM leader,`user` WHERE leader.user_id=user.user_ID;";
            $result=$db->queryData($queryStr);
            if(!$result){
                $leaderDataAll['state']=-1;
            }
            else{
                $leaderDataAll['state']=1;
                while ($temp=$result->fetch_assoc()){
                    $leaderDataAll['data'][]=array(
                        user_id=>$temp['user_id'],
                        user_name=>$temp['user_name']
                    );
                }
            }
            return $leaderDataAll;
        }
        /*
         * 获取一个人所有数据
         */
        public function  getLeaderAllUserDataById($user_id){
            $db=new SQLBase();
            $queryStr="SELECT leader.user_id,leader.section_id,`user`.user_name,leader.is_approve,leader.is_fee,leader.power_level, allsection.section_name 
                      FROM leader,user,allsection WHERE leader.user_id=user.user_ID AND  leader.user_id='$user_id' AND leader.section_id = allsection.section_ID";
            $result=$db->queryData($queryStr);
// 	echo $queryStr;
            if(!$result){
                $leaderDataAll['state']=-1;
            }
            else{
                $leaderDataAll['state']=1;
                while ($temp=$result->fetch_assoc()){
                    $leaderDataAll['data'][]=array(
                        user_id=>$temp['user_id'],
                        section_id=>$temp['section_id'],
                        section_name=>$temp['section_name'],
                        user_name=>$temp['user_name'],
                        is_approve=>$temp['is_approve'],
                        is_fee=>$temp['is_fee'],
                        power_level=>$temp['power_level']
                    );
                }
            }
            return $leaderDataAll;
        }
        /*
         * addleader
         * 增加数据
         */
        public function addLeader($data){
            $db =new SQLBase();
            $queryStr='INSERT INTO leader(user_id) VALUES('.$data['user_id'].');';
// 	echo $queryStr;
            $result=$db->queryData($queryStr);
            if(!$result){
                $resultArrayaAll['state']=-1;
            }else{
                $resultArrayaAll['state']=1;
            }
            return $resultArrayaAll;
        }
        /*
         * 搜索数据
         */
        public function searchLeaderDataById($userId){
            $db=new SQLBase();
            $queryStr="SELECT leader.user_id,allsection.section_name,leader.is_fee,leader.is_approve,leader.power_level, leader.section_id 
                      FROM user,leader,allsection where leader.user_id=`user`.user_ID AND leader.section_id=allsection.section_ID AND allsection.section_name='".$userId."';";
            $result=$db->queryData($queryStr);
// 	echo $queryStr;
            if(!$result){
                $leaderData['state']=-1;
            }
            else {
                while ($temp=$result->fetch_assoc()){
                    $leaderData['user_id']=$temp[''];
                    $leaderData['section_name']=$temp['section_name'];
                    $leaderData['is_fee']=$temp['is_fee'];
                    $leaderData['is_approve']=$temp['is_approve'];
                    $leaderData['power_level']=$temp['power_level'];
                    $leaderDataAll[]=$leaderData;
                }
            }
            return $leaderDataAll;
        }

        /*
         * 插入数据
         */
// function insertLeaderDataById($userId,$update){
// 	$db=new SQLBase();leader()";
// }
// 	$queryStr="INSERT INTO

// /*
//  * 更新数据
//  */
    public function updateLeaderUserData($data){
        $countLeader=count($data['data']);
        $leaderData=$data['data'];
        $db=new SQLBase();
        $queryStr="DELETE FROM leader WHERE user_id=".$data['user_id'].';';
        $result=$db->queryData($queryStr);
// 	echo
        if(!$result){
            $resultArrayALL['state']=-1;
        }
        else{
            if($countLeader==0) $resultArrayALL['state']=1;
            else{
                for($i=0;$i<$countLeader;$i++){
                    $queryStr="INSERT INTO leader(user_id,section_id,is_approve,is_fee,power_level) VALUES(";
                    $queryStr.=$leaderData[$i]['user_id'].',';
                    $queryStr.='"'.$leaderData[$i]['section_id'].'",';
                    $queryStr.=$leaderData[$i]['is_approve'].',';
                    $queryStr.=$leaderData[$i]['is_fee'].',';
                    $queryStr.=$leaderData[$i]['power_level'].');';
// 			echo $queryStr;
                    $result=$db->queryData($queryStr);
                    if(!$result){
                        $resultArrayALL['state']=-1;
                    }else {
                        $resultArrayALL['state']=1;
                    }
                }
            }
        }
        return  $resultArrayALL;
    }
    /*
     * 获取某一个人的某个部门信息
     */
    public function getLeaderDataByUserAndSection($leaderData){
        $db =new SQLBase();
        $queryStr="SELECT leader.power_level,leader.is_approve,leader.is_fee,allsection.section_name FROM leader,allsection WHERE 
               allsection.section_ID=leader.section_id AND leader.user_id=".$leaderData['user_id'].' 
               AND leader.section_id='.$leaderData['section_id'].';';
        $result=$db->queryData($queryStr);
// 	echo $queryStr;
        if(!$result){
            $resultArrayAll['state']=-1;
        }else {
            $resultArrayAll['state']=1;
            while($temp=$result->fetch_assoc())
                $resultArrayAll['data'][]=array(
                    power_level=>$temp['power_level'],
                    is_approve=>$temp['is_approve'],
                    is_fee=>$temp['is_fee'],
                    section_name=>$temp['section_name']
                );
        }
        return $resultArrayAll;
    }
    /*
     * 更新个人数据依据用户ID和部门ID
     */
    public function updateLeaderDataByUserAndSection($update){
        $db =new SQLBase();
        $queryStr='UPDATE leader SET power_level='.$update['power_level'].', is_approve='.$update['is_approve'].', is_fee='.$update['is_fee'].' WHERE user_id='.$update['user_id'].' AND section_id="'.$update['section_id'].'";';
        $result=$db->queryData($queryStr);
        if(!$result){
            $resultArrayAll['state']=-1;
        }else {
            $resultArrayAll['state']=1;
        }
        return $resultArrayAll;
    }
    /*
     * 删除个人数据依据用户ID和部门ID
     */
    public  function deleteLeaderSectionByUserAndSection($leaderData){
        $db =new SQLBase();
        $queryStr='DELETE FROM leader WHERE user_id='.$leaderData['user_id'].' AND section_id="'.$leaderData['section_id'].'";';
        $result=$db->queryData($queryStr);
//  	echo $queryStr;
        if(!$result){
            $resultArrayAll['state']=-1;
        }else {
            $resultArrayAll['state']=1;
        }
        return $resultArrayAll;
    }
    /*
     * 得到一个人所有部门
     */
    public function getLeaderAllSectionId($user_id){
        $db=new SQLBase();
        $queryStr='SELECT * FROM allsection where section_ID not in(select section_id from leader where user_id="'.$user_id.'");';
        $result=$db->queryData($queryStr);
//  	echo $queryStr;
        if(!$result){
            $resultArrayAll['state']=-1;
        }else{
            $resultArrayAll['state']=1;
            while ($temp=$result->fetch_assoc()){
                $resultArrayAll['data'][]=array(
                    section_id=>$temp['section_ID'],
                    section_name=>$temp['section_name']
                );
            }
        }
        return $resultArrayAll;
    }
    /*
     * 插入个人数据依据用户ID和部门ID
     */
    public function insertLeaderSection($leaderData){
        $db=new SQLBase();
        $queryStr='INSERT INTO leader(user_id,section_id,power_level,is_approve,is_fee) VALUES(';
        $queryStr.=$leaderData['user_id'].',';
        $queryStr.=$leaderData['section_id'].',';
        $queryStr.=$leaderData['power_level'].',';
        $queryStr.=$leaderData['is_approve'].',';
        $queryStr.=$leaderData['is_fee'].');';
        $result=$db->queryData($queryStr);
        if(!$result){
            $resultArrayAll['state']=-1;
        }else {
            $resultArrayAll['state']=1;
        }
        return $resultArrayAll;
    }

    function getSectionAdmin($sectionID, $userID){
        $conn = new SQLBase();
        $query = "SELECT * FROM leader WHERE user_id = '$userID' AND section_id = $sectionID AND power_level = 1";
        $r = $conn->queryData($query);
        $result_array = array();
        if($r){
            $result_array['state'] = 1;
            if($r->num_rows > 0){
                $row = $r->fetch_array();
                $result_array['is_approve'] = $row['is_approve'];
                $result_array['is_fee'] = $row['is_fee'];
            }
            else{
                $result_array['is_approve'] = 0;
                $result_array['is_fee'] = 0;
            }
        }
        else{
            $result_array['state'] = 0;
        }
        return $result_array;
    }
}


?>